package com.hsytech.dijiebao.expand.jpush;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.hsytech.dijiebao.home.alldetail.HotelDetailActivity;
import com.hsytech.dijiebao.home.alldetail.MyselfHotelDetailActivity;
import com.hsytech.dijiebao.home.alldetail.MyselfTaxiDetailActivity;
import com.hsytech.dijiebao.home.alldetail.TaxiDetailActivity;
import com.hsytech.dijiebao.person.dao.InformationDao;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;

public class JpushReceiver extends BroadcastReceiver {

	private static final String TAG = "JPush";
	private String extras;
	private String alert;
	private String id;
	private String status;
	private String type;
	private Map<String, Object> map = new HashMap<String, Object>();

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		extras = (String) bundle.get(JPushInterface.EXTRA_EXTRA);

		alert = (String) bundle.get(JPushInterface.EXTRA_ALERT);

		if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
			String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
			Log.d(TAG, "[MyReceiver] ����Registration Id : " + regId);
			// send the Registration Id to your server...

		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
			Log.d(TAG, "[MyReceiver] ���յ������������Զ�����Ϣ: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
			// processCustomMessage(context, bundle);

		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
			Log.d(TAG, "[MyReceiver] ���յ�����������֪ͨ");
			int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
			Log.d(TAG, "[MyReceiver] ���յ�����������֪ͨ��ID: " + notifactionId);

		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
			Log.d(TAG, "[MyReceiver] �û��������֪ͨ");
			Log.d(TAG, "[MyReceiver] ����Registration Id : " + bundle);
			try {
				if (!extras.equals("") && extras != null && !alert.equals("") && alert != null) {

					JSONObject respones = new JSONObject(extras);
					Log.d(TAG, "respones : " + respones);
					// String carOrHotle =
					// respones.optJSONObject("android").optJSONObject("extras").optString("model");
					status = respones.optString("status");
					type = respones.optString("type");
					id = respones.optString("id");
					map.put("id", id);
					Log.d(TAG, "�������ݿ��id" + id);
					map.put("carorhotle", type);
					Log.d(TAG, "�������ݿ��type" + type);

					map.put("status", status);
					Log.d(TAG, "�������ݿ��status" + status);

					// if (!alert.equals("") && alert != null) {
					map.put("content", alert);
					Log.d(TAG, "�������ݿ�ĳ���" + map.size());
					InformationDao.informationDao.add(map);
					// }
				}

			} catch (JSONException e) {
				// e.printStackTrace();
				Log.d(TAG, e.getMessage());
			}

			// //���Զ����Activity
			// �����ҳ�
			if (type.equals("1") && status.equals("1")) {

				Intent TaxiDetail = new Intent(context, TaxiDetailActivity.class);
				TaxiDetail.putExtra("idTaxiHelp", id);
				TaxiDetail.putExtra("collectTaxi", "1");
				// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				TaxiDetail.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(TaxiDetail);
			}

			// ���г���
			if (type.equals("1") && status.equals("2")) {

				Intent MyselfTaxiDetail = new Intent(context, MyselfTaxiDetailActivity.class);
				MyselfTaxiDetail.putExtra("idTaxi", id);
				MyselfTaxiDetail.putExtra("collectMyTaxi", "1");
				// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				MyselfTaxiDetail.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(MyselfTaxiDetail);
			}
			// �����Ƶ�
			if (type.equals("2") && status.equals("1")) {
				Intent HotelDetail = new Intent(context, HotelDetailActivity.class);
				HotelDetail.putExtra("idHelp", id);
				HotelDetail.putExtra("collectHotle", "1");
				// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				HotelDetail.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(HotelDetail);
			}
			// ����Ƶ�
			if (type.equals("2") && status.equals("2")) {
				Intent MyselfHotelDetail = new Intent(context, MyselfHotelDetailActivity.class);
				MyselfHotelDetail.putExtra("id", id);
				MyselfHotelDetail.putExtra("collectMyHotle", "1");
				// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				MyselfHotelDetail.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(MyselfHotelDetail);
			}
		} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
			Log.d(TAG, "[MyReceiver] �û��յ���RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
			// ��������� JPushInterface.EXTRA_EXTRA �����ݴ�����룬������µ�Activity��
			// ��һ����ҳ��..

		} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
			boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
			Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
		} else {
			Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
		}
	}

	// ��ӡ���е� intent extra ����
	private static String printBundle(Bundle bundle) {
		StringBuilder sb = new StringBuilder();
		for (String key : bundle.keySet()) {
			if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
				sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
			} else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
				sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
			} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
				if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
					Log.i(TAG, "This message has no Extra data");
					continue;
				}

				try {
					JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
					Iterator<String> it = json.keys();

					while (it.hasNext()) {
						String myKey = it.next().toString();
						sb.append("\nkey:" + key + ", value: [" + myKey + " - " + json.optString(myKey) + "]");
					}
				} catch (JSONException e) {
					Log.e(TAG, "Get message extra JSON error!");
				}

			} else {
				sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
			}
		}
		return sb.toString();
	}

	// send msg to MainActivity
	// private void processCustomMessage(Context context, Bundle bundle) {
	// if (MainActivity.isForeground) {
	// String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
	// String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
	// Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
	// msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
	// if (!ExampleUtil.isEmpty(extras)) {
	// try {
	// JSONObject extraJson = new JSONObject(extras);
	// if (null != extraJson && extraJson.length() > 0) {
	// msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
	// }
	// } catch (JSONException e) {
	//
	// }
	//
	// }
	// context.sendBroadcast(msgIntent);
	// }
	// }

}
